package com.work.game.home.ui.activity;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.work.game.R;
import com.work.game.config.UrlAddressConfig;
import com.work.game.utils.VersionUtils;
import com.work.game.utils.risk.DeviceUtils;
import com.work.game.volley.BaseActivity;
import com.zzhoujay.richtext.RichText;

import java.util.List;

public class SetUpActivity extends BaseActivity {

    private TextView tvVersionName, tvName,tvNickname,tvId;

    private AlertDialog alertDialog, alertDialogSure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_set_up_style2);
        super.onCreate(savedInstanceState);
        tvActionbarTitle.setText("设置");
        ivActionbarLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvVersionName.setText("版本V" + VersionUtils.getAppVersionName(this));
        tvName.setText("关于" + getResources().getString(R.string.app_name));

    }

    @Override
    public void initClick() {
        super.initClick();
        ImmersionBar.with(this).titleBar(findViewById(R.id.layout_title)).init();
        findViewById(R.id.ll_summary).setOnClickListener(this);
        findViewById(R.id.ll_privacy_policy).setOnClickListener(this);
        findViewById(R.id.ll_protecting_children).setOnClickListener(this);
        findViewById(R.id.ll_user_agreement).setOnClickListener(this);
        findViewById(R.id.ll_sdk).setOnClickListener(this);
        findViewById(R.id.ll_detailed_list).setOnClickListener(this);
        findViewById(R.id.ll_permission_list).setOnClickListener(this);
        findViewById(R.id.ll_setup).setOnClickListener(this);
        findViewById(R.id.ll_withdraw).setOnClickListener(this);
        findViewById(R.id.ll_feedback).setOnClickListener(this);
        findViewById(R.id.ll_delete).setOnClickListener(this);
        tvVersionName = findViewById(R.id.tv_version_name);
        tvName = findViewById(R.id.tv_name);
        tvNickname = findViewById(R.id.tv_nickname);
        tvId = findViewById(R.id.tv_id);
        tvNickname.setText(getSharedPreferences("nickname"));
        tvId.setText(getSharedPreferences("id"));
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_summary:
                sendIntent(this, WebViewActivity.class, "隐私政策概要");
                break;
            case R.id.ll_protecting_children:
                sendIntent(this, WebViewActivity.class, "儿童隐私保护声明");
                break;
            case R.id.ll_privacy_policy:
                sendIntent(this, WebViewActivity.class, "隐私政策");
                break;
            case R.id.ll_user_agreement:
                sendIntent(this, WebViewActivity.class, "用户协议");
                break;
            case R.id.ll_sdk:
                sendIntent(this, WebViewActivity.class, "第三方SDK清单");
                break;
            case R.id.ll_detailed_list:
                sendIntent(this, WebViewActivity.class, "个人信息收集清单");
                break;
            case R.id.ll_permission_list:
                sendIntent(this, WebViewActivity.class, "权限列表");
                break;
            case R.id.ll_setup:
                try {
                    // 构造Uri
                    Uri uri = Uri.parse("package:" + DeviceUtils.getInstance().getPackageName(this));
                    // 创建Intent
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, uri);
                    // 启动Intent
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    // 如果找不到对应的Activity，可能是用户没有安装该应用或系统不支持
                    Toast.makeText(this, "无法找到该应用", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.ll_withdraw:
                showWithdraw();
                break;
            case R.id.ll_feedback:
                sendIntent(this, ProblemFeedbackActivity.class);
                break;
            case R.id.ll_delete:
                sendIntent(this, DeleteUserActivity.class);
                break;
        }
    }

    private void showWithdraw() {
        //判断提现账号
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        // dialog.setTitle("绑定姓名");
        View root = LayoutInflater.from(this).inflate(R.layout.pop_withdraw, null);
        dialog.setView(root);

        root.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showWithdrawSure();
            }
        });
        root.findViewById(R.id.tv_no).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        alertDialog = dialog.create();
        alertDialog.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialog.setCancelable(false);
        alertDialog.show();
    }

    private void showWithdrawSure() {
        //判断提现账号
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        // dialog.setTitle("绑定姓名");
        View root = LayoutInflater.from(this).inflate(R.layout.pop_withdraw_sure, null);
        dialog.setView(root);
        TextView tv = root.findViewById(R.id.tv_web);
        String content = "根据国家规定，需同意协议内容方可进入应用，您可以通过阅读完整版<a href=\"" + UrlAddressConfig.URL_WEB_VIEW + "/setup/用户协议.html\">《用户协议》</a>和<a href=\"" + UrlAddressConfig.URL_WEB_VIEW + "/setup/隐私政策.html\">《隐私政策》</a>了解详细信息";
        RichText.fromHtml(content).into(tv);
        root.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialogSure.dismiss();
                alertDialog.dismiss();
            }
        });
        root.findViewById(R.id.tv_no).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setSharedPreferences("deviceId", "");
                finishAffinity();
            }
        });
        alertDialogSure = dialog.create();
        alertDialogSure.getWindow().setBackgroundDrawableResource(R.color.transparent);
        alertDialogSure.setCancelable(false);
        alertDialogSure.show();
    }
}